﻿using System;
using System.IO;
using System.Windows.Media.Imaging;

namespace MediaAssistant.DAL.Helper
{
    public class ConvertTo
    {
        public static string String(TimeSpan timeSpan)
        {
            var totalSeconds = Math.Abs(timeSpan.TotalSeconds);
            var totalMinutes = Math.Abs(timeSpan.TotalMinutes);
            var hours = Math.Abs(timeSpan.TotalHours);
            if (totalSeconds > 0)
            {
                var seconds = Convert.ToInt16(totalSeconds%60);
                var minutes = Convert.ToInt16(totalMinutes%60);
                var totalHours = Convert.ToInt32(hours);
                var text = System.String.Empty;
                if (timeSpan.TotalMilliseconds < 0)
                    text += "-";
                if (totalHours > 0)
                    text += System.String.Format("{0:00}:", totalHours);
                text += System.String.Format("{0:00}:{1:00}", minutes, seconds);
                return text;
            }
            return "00:00";
        }

        public static BitmapImage BitmapImage(byte[] bytes)
        {
            try
            {
                if (bytes == null || bytes.Length == 0)
                    return null;
                var bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(bytes);
                bmp.EndInit();
                return bmp;
            }
            catch (Exception)
            {
                return null;
            }
            
        }
    }
}
